const router = require('koa-router')();
const { sequelize } = require('../common/dbs')

const roleModel = require("../models/role")
const roleInfo = roleModel(sequelize);

var Sequelize = require('sequelize')
var Op = Sequelize.Op;


// 查询分页列表
router.get('/pageList', async (ctx) => {
    try {
        let reqQuery = ctx.request.query;
        let reData = await roleInfo.findAndCountAll(
            {
                where: {
                    name: {
                        [Op.like]: `%${reqQuery.name||''}%`
                    },
                },
                order: [  //排序 DESC ASC
                    ['created_at', 'DESC']
                ],
                limit: reqQuery.limit ? parseInt(reqQuery.limit) : 10,
                offset: reqQuery.page > 0 ? (parseInt(reqQuery.page) - 1) * 10 : 0
            }
        );
        ctx.success(reData)
    } catch (err) {
        ctx.fail(0, err)
    }
})



/**
 * 编辑或者添加
 * */

router.post('/editOrAdd', async (ctx) => {
    try {
        ctx
            .validateBody("name")
            .required("角色名称不能为空")
            .isString()
            .trim()
        ctx
            .validateBody("menuIds")
            .required("菜单不能为空")
            .isString()
            .trim()

        let reqBody = ctx.request.body;
        let requestData = null
        let msgType = '添加'
        let b = {
            name: reqBody.name,
            menuIds: reqBody.menuIds,
            remark: reqBody.remark,
            status: reqBody.status,
        }

        if (reqBody.id) {
            delete b.id
            // 编辑
            requestData = await roleInfo.update(b, {
                where: {
                    id: reqBody.id
                }
            })
            msgType = '修改'
        } else {
            // 新增
            requestData = await roleInfo.create(b)
        }
        if (requestData) {
            ctx.success(msgType + '成功')
        } else {
            ctx.fail(0, msgType + '失败')
        }

    } catch (err) {
        ctx.fail(0, err && err.message)
    }
})

// 查询列表
router.get('/list', async (ctx) => {
    try {
        let reqQuery = ctx.request.query;
        let reData = await roleInfo.findAll(
            {

                order: [  //排序 DESC ASC
                    ['created_at', 'DESC']
                ]
            }
        );
        ctx.success(reData)
    } catch (err) {
        ctx.fail(0, err)
    }
})

/**
 * 详情
 * */

router.get('/detail', async (ctx) => {
    try {
        let reqQuery = ctx.request.query;
        if (!reqQuery.id) {
            ctx.fail(0, 'id不能为空')
        }
        let cardData = await roleInfo.findOne(
            {
                where: {
                    id: reqQuery.id
                },
            }
        );

        if (cardData) {
            ctx.success(cardData)
        } else {
            ctx.fail(0, '内容已删除,或者不存在')
        }
    } catch (err) {
        ctx.fail(0, err)
    }
})

// 查询列表
router.get('/list', async (ctx) => {
    try {
        let reqQuery = ctx.request.query;
        let reData = await roleInfo.findAll(
            {
                order: [  //排序 DESC ASC
                    ['created_at', 'DESC']
                ]
            }
        );
        ctx.success(reData)
    } catch (err) {
        ctx.fail(0, err)
    }
})


// 删除
router.delete('/del/:id', async (ctx) => {
    try {
        if (!ctx.params.id) {
            ctx.fail(0, 'id不能为空')
        }
        let requestData = await roleInfo.destroy({
            where: {
                id: ctx.params.id
            }
        })
        if (requestData > 0) {
            ctx.success('删除成功')
        } else {
            ctx.fail(0, '内容已删除,或者不存在')
        }
    } catch (err) {
        ctx.fail(0, err)
    }
})
module.exports = router.routes();

